package com.bootdo.reptile;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.reptile.inteface.TaskConsumer;
import com.bootdo.reptile.inteface.imp.Task;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import io.netty.util.NettyRuntime;
import io.netty.util.internal.SystemPropertyUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.*;

/**
  *
  *爬虫工厂
　* @author 郝江波
　* @date 2019/9/3 14:22
　*/
public  class ReptileFactory {

    private static ReptileFactory instance = new ReptileFactory();

    //最小线程数
    //借用netty一段源码  获取当前cpu数量 * 2
    private static final int MIN = SystemPropertyUtil.getInt(
            "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2);
    //限制为线程池线程最大数量
    private static final int MAX = SystemPropertyUtil.getInt(
            "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 5);
    //线程自动回收时间 1秒
    private static final int TIMEOUT = 1;

    //创建线程池 此处不提供配置  默认配置即可
    private static final ThreadPoolExecutor threadFactory
            = new ThreadPoolExecutor
            (MIN, MAX, TIMEOUT, TimeUnit.SECONDS,
                    new LinkedBlockingQueue());


    public  void runTask(String url,TaskConsumer taskConsumer){
        runTask(url,taskConsumer,null,0);
    }



    public  void runTask(String url,TaskConsumer taskConsumer,String proxyIp, int port){
        threadFactory.execute(()->{
            String str = null;
            WebClient wc = new WebClient();
            try {
                //设置代理
                if(StringUtils.isNotEmpty(proxyIp)){
                    ProxyConfig proxyConfig = wc.getOptions().getProxyConfig();
                    proxyConfig.setProxyHost(proxyIp);
                    proxyConfig.setProxyPort(port);
                }
                wc.getOptions().setJavaScriptEnabled(true); //启用JS解释器，默认为true
                wc.getOptions().setCssEnabled(false); //禁用css支持
                wc.getOptions().setThrowExceptionOnScriptError(false); //js运行错误时，是否抛出异常
                wc.getOptions().setTimeout(10000); //设置连接超时时间 ，这里是10S。如果为0，则无限期等待
                HtmlPage page = wc.getPage(url);
                str = page.asXml(); //以xml的形式获取响应文本
                taskConsumer.call(str, ReptileFactory.this,url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static ReptileFactory getInstance(){
        return instance;
    }


}
